Package org.jessma.hpsocket.unicode

Source Code of org.jessma.hpsocket.unicode.TcpClient

/*
* Copyright: JessMA Open Source (ldcsaa@gmail.com)
*
* Version  : 3.2.3
* Author  : Bruce Liang
* Website  : http://www.jessma.org
* Project  : https://github.com/ldcsaa
* Blog    : http://www.cnblogs.com/ldcsaa
* Wiki    : http://www.oschina.net/p/hp-socket
* QQ Group  : 75375912
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jessma.hpsocket.unicode;

import org.jessma.hpsocket.Buffer;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.WString;

/** TCP Client 组件基类 (Unicode) */
public class TcpClient extends Client
{
  /** 创建组件对象 */
  public static TcpClient create(Mode mode)
  {
    TcpClient instance  = new TcpClient();
    instance.mode    = mode;
   
    switch(mode)
    {
    case PUSH:
      instance.socketListener  = HPSocket.SDK.Create_HP_TcpClientListener();
      instance.socketObj    = HPSocket.SDK.Create_HP_TcpClient(instance.socketListener);
      break;
    case PULL:
      instance.socketListener  = HPSocket.SDK.Create_HP_TcpPullClientListener();
      instance.socketObj    = HPSocket.SDK.Create_HP_TcpPullClient(instance.socketListener);
      break;
    default:
      assert false;
    }
   
    return instance;
  }
 
  /** 销毁组件对象 */
  public static void destroy(TcpClient client)
  {
    if(client.socketObj == null && client.socketListener == null)
      return;
   
    synchronized(client)
    {
      switch(client.mode)
      {
      case PUSH:
        if(client.socketObj != null)
          HPSocket.SDK.Destroy_HP_TcpClient(client.socketObj);
        if(client.socketListener != null)
          HPSocket.SDK.Destroy_HP_TcpClientListener(client.socketListener);
       
        break;
       
      case PULL:
        if(client.socketObj != null)
          HPSocket.SDK.Destroy_HP_TcpPullClient(client.socketObj);   
        if(client.socketListener != null)
          HPSocket.SDK.Destroy_HP_TcpPullClientListener(client.socketListener);
       
        break;
       
      default:
        assert false;
      }
     
      client.clearReferences();
    }
  }
 
  protected TcpClient()
  {
   
  }
 
  @Override
  protected void finalize() throws Throwable
  {
    destroy(this);
    super.finalize();
  }

  /* ********************************* TCP Client 组件操作方法 ****************************** */
 
  /** 发送小文件 */
  public boolean sendSmallFile(String lpszFileName, byte[] pHead, byte[] pTail)
  {
    Buffer headBuffer = pHead != null ? new Buffer(pHead) : null;
    Buffer tailBuffer = pTail != null ? new Buffer(pTail) : null;
   
    return sendSmallFile(lpszFileName, headBuffer, tailBuffer);
  }

  /** 发送小文件 */
  public boolean sendSmallFile(String lpszFileName, Buffer pHead, Buffer pTail)
  {
    long pMemHead    = 0;
    long pMemTail    = 0;
    Pointer pHeadBuffer = null;
    Pointer pTailBuffer = null;
   
    if(pHead != null)
    {
      pMemHead  = Native.malloc(Buffer.NATIVE_BUFFER_SIZE);
      pHeadBuffer  = new Pointer(pMemHead);
      pHeadBuffer.setInt(0, pHead.getLength());
      pHeadBuffer.setPointer(Pointer.SIZE, pHead.getPointer());
    }

    if(pTail != null)
    {
      pMemTail  = Native.malloc(Buffer.NATIVE_BUFFER_SIZE);
      pTailBuffer  = new Pointer(pMemTail);
      pTailBuffer.setInt(0, pTail.getLength());
      pTailBuffer.setPointer(Pointer.SIZE, pTail.getPointer());
    }

    WString lpwszFileName = lpszFileName != null ? new WString(lpszFileName) : null;
    boolean result = HPSocket.SDK.HP_TcpClient_SendSmallFile(socketObj, lpwszFileName, pHeadBuffer, pTailBuffer);
   
    if(pMemHead != 0) Native.free(pMemHead);
    if(pMemTail != 0) Native.free(pMemTail);
   
    return result;
  }
 
  /* ******************************* TCP Client 属性访问方法 ******************************* */
 
  /** 设置通信数据缓冲区大小(根据平均通信数据包大小调整设置,通常设置为:(N * 1024) - sizeof(TBufferObj)) */
  public void setSocketBufferSize(int dwSocketBufferSize)
  {
    HPSocket.SDK.HP_TcpClient_SetSocketBufferSize(socketObj, dwSocketBufferSize);
  }
 
  /** 设置心跳包间隔(毫秒,0 则不发送心跳包) */
  public void setKeepAliveTime(int dwKeepAliveTime)
  {
    HPSocket.SDK.HP_TcpClient_SetKeepAliveTime(socketObj, dwKeepAliveTime);
  }
 
  /** 设置心跳确认包检测间隔(毫秒,0 不发送心跳包,如果超过若干次 [默认:WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线) */
  public void setKeepAliveInterval(int dwKeepAliveInterval)
  {
    HPSocket.SDK.HP_TcpClient_SetKeepAliveInterval(socketObj, dwKeepAliveInterval);
  }

  /** 获取通信数据缓冲区大小 */
  public int getSocketBufferSize()
  {
    return HPSocket.SDK.HP_TcpClient_GetSocketBufferSize(socketObj);
  }
 
  /** 获取心跳检查次数 */
  public int getKeepAliveTime()
  {
    return HPSocket.SDK.HP_TcpClient_GetKeepAliveTime(socketObj);
  }
 
  /** 获取心跳检查间隔 */
  public int getKeepAliveInterval()
  {
    return HPSocket.SDK.HP_TcpClient_GetKeepAliveInterval(socketObj);
  }

  /* ***************************** TCP Pull Client 组件操作方法 **************************** */
 
  /**
  * 抓取数据
  */
  public int fetch(NativeLong dwConnID, byte[] pBuffer, int iLength)
  {
    return HPSocket.SDK.HP_TcpPullClient_Fetch(socketObj, dwConnID, pBuffer, iLength);
  }

}
TOP

Related Classes of org.jessma.hpsocket.unicode.TcpClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.